﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Dict.Blo;
using Dict.DataModel;

namespace UI
{
    public class TalentPage : MonoBehaviour
    {
        public GameObject scrollView;
        public List<GameObject> talentItems;
        public List<GameObject> branchTalentItems;
        public TalentDetail talentDetail;
        public GameObject activeParticlePref;
        public GameObject activeParticle2;

        Dictionary<string, GameObject> trunkItems = new Dictionary<string, GameObject>();
        Dictionary<string, GameObject> branchItems = new Dictionary<string, GameObject>();

        UIHero heroInfo;
        GameObject lastChosedItem;
        GameObject activeParticle;

        void Init()
        {
            foreach (var item in talentItems)
            {
                item.SetActive(false);
            }
            foreach (var item in branchTalentItems)
            {
                item.SetActive(false);
            }

            int i = 0;
            int j = 0;
            //string lastKey = "";
            List<TableTalentAdvenced> talentAdvData = TableTalentBlo.GetTalentAdvanceData(heroInfo.heroId);
            foreach (var item in talentAdvData)
            {
                if (item.trunck == 0)
                {
                    if(j < branchTalentItems.Count)
                    {
                        branchTalentItems[j].SetActive(true);
                        branchItems[item.talentLevel] = branchTalentItems[j];
                        j++;
                    }
                }
                else if(i < talentItems.Count && item.talentLevel != "0")
                {
                    talentItems[i].SetActive(true);
                    talentItems[i].GetComponent<TalentItem>().haveChildren = TableTalentBlo.IsTalentHaveChildren(talentAdvData, item.talentLevel);
                    talentItems[i].GetComponent<TalentItem>().haveBrunchChildren = TableTalentBlo.IsTalentHaveBrunchChildren(talentAdvData, item.talentLevel);
                    trunkItems[item.talentLevel] = talentItems[i];
                    //lastKey = item.talentLevel;
                    i++;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="guid"></param>
        public void SetHero(string guid)
        {
            heroInfo = StaticData.playerData.GetUIHero(guid);
            
            Init();

            if (activeParticle != null)
                activeParticle.SetActive(false);

            if (activeParticle2 != null)
                activeParticle2.SetActive(false);

            UpdateAll();
        }

        public void OnTalentClick(UITalent talent)
        {
            if (lastChosedItem != null)
                lastChosedItem.GetComponent<TalentItem>().SetChosed(false);
            if (trunkItems.ContainsKey(talent.data.talentLevel))
            {
                trunkItems[talent.data.talentLevel].GetComponent<TalentItem>().SetChosed(true);
                lastChosedItem = trunkItems[talent.data.talentLevel];
            }
            if (branchItems.ContainsKey(talent.data.talentLevel))
            {
                branchItems[talent.data.talentLevel].GetComponent<TalentItem>().SetChosed(true);
                lastChosedItem = branchItems[talent.data.talentLevel];
            }

            talentDetail.SetTalent(talent);
        }

        public void UpdateAll()
        {
            //更新天赋，
            bool first = false;
            UITalent firstChose = null;
            int count = 0;
            foreach (var item in heroInfo.TalentData)
            {
                if (item.IsTrunk && trunkItems.ContainsKey(item.data.talentLevel))
                {
                    trunkItems[item.data.talentLevel].GetComponent<TalentItem>().SetTalent(item);

                    //处理默认选谁
                    trunkItems[item.data.talentLevel].GetComponent<TalentItem>().SetChosed(false);
                    trunkItems[item.data.talentLevel].GetComponent<TalentItem>().canClick = true;
                    if (!item.isActived && !first)
                    {
                        first = true;
                        firstChose = item;
                        //trunkItems[item.data.talentLevel].GetComponent<TalentItem>().canClick = true;
                    }
                    if (item.isActived || item.isUnlocked)
                    {
                        //trunkItems[item.data.talentLevel].GetComponent<TalentItem>().canClick = true;
                    }
                    count++;
                    if (count == trunkItems.Count && !first)
                        firstChose = item;
                }
                else if (branchItems.ContainsKey(item.data.talentLevel))
                {
                    branchItems[item.data.talentLevel].GetComponent<TalentItem>().SetTalent(item);
                    branchItems[item.data.talentLevel].GetComponent<TalentItem>().SetChosed(false);
                    branchItems[item.data.talentLevel].GetComponent<TalentItem>().canClick = true;

                    if (item.isActived || item.isUnlocked)
                    {
                        //branchItems[item.data.talentLevel].GetComponent<TalentItem>().canClick = true;
                    }

                    //分支路线 亮暗处理
                    for (int i = 0; i < item.data.parent_array.Length; i++)
                    {
                        if (trunkItems.ContainsKey(item.data.parent_array[i]))
                        {
                            trunkItems[item.data.parent_array[i]].GetComponent<TalentItem>().SetBranchPath(item.isActived || item.isUnlocked);
                        }
                    }
                }
            }

            //默认选中下一个要激活的天赋
            if (firstChose.IsTrunk)
                trunkItems[firstChose.data.talentLevel].GetComponent<TalentItem>().SetChosed(true);
            else
                branchItems[firstChose.data.talentLevel].GetComponent<TalentItem>().SetChosed(true);
            OnTalentClick(firstChose);

            if (firstChose.IsTrunk && int.Parse(firstChose.data.talentLevel) > 6 || !firstChose.IsTrunk && int.Parse(firstChose.data.parent) > 6)
                scrollView.GetComponent<UIScrollView>().SetDragAmount(0, 277, false);
            else
                scrollView.GetComponent<UIScrollView>().ResetPosition();
        }

        //播放激活特效
        public void PlayActiveParticle()
        {
            if(activeParticle == null)
            {
                //GameObject obj = AssetLoad.ResourceManager.Instance.LoadResource("particles/UI_maofei/tianfu_jihuo_donghua") as GameObject;
                activeParticle = Instantiate(activeParticlePref);
            }

            activeParticle.transform.parent = lastChosedItem.transform;
            activeParticle.transform.localScale = Vector3.one;
            activeParticle.transform.localPosition = new Vector3(86, -30, 0);

            StopCoroutine("StopParticle");
            StartCoroutine("StopParticle");
            AudioManager.Instance.PlayUIAudio("juesetianfu");
            activeParticle.SetActive(true);
            activeParticle2.SetActive(true);
        }

        IEnumerator StopParticle()
        {
            yield return new WaitForSeconds(1.5f);
            UpdateAll();
            activeParticle.SetActive(false);
            activeParticle2.SetActive(false);
        }
    }
}
